home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 / Ham Radio 2000.iso / ham2000 / packet / monax25 / sparse.c < prev    next >
Text File  |  1987-10-20  |  9KB  |  432 lines

  1. /* sparse.c - Parser for STATS tool.
  2.    This module is part of stats.exe
  3.                   
  4.    Language = Microsoft C version 4.0
  5.  
  6.  
  7.    This source is distributed freely and may be copied and
  8.    redistributed with the following provisos:
  9.    
  10.            You may not sell it, nor may you charge for making 
  11.            copies beyond the actual cost of mailing and media.
  12.                       
  13.    Written by Skip Hansen WB6YMH and Harold Price NK6K.
  14.  
  15.    Feedback is desired.
  16.  
  17.    RCP/M (213) 541-2503 300/1200/2400 baud
  18.    or via packet WB6YMH @ WB6YMH-2 or 
  19.          NK6K @ NK6K
  20.  
  21.    Modification history:
  22.  
  23.     8/10/87         NK6K: Initial release.    
  24.     ver 1.0         
  25.  
  26.    10/18/87     NK6K: First general release.
  27.    ver 1.1
  28.   
  29.  
  30.     Notes:  This was a quick grab from something else that was
  31.         laying around, and has several hooks for things 
  32.         which aren't actually here.  On the other hand, it
  33.         does the job.  To add new commands, add the text to
  34.         sparse.h and a new case in doit() below.
  35.  
  36. */
  37.  
  38.  
  39.  
  40. #include "sparse.h"
  41. #include <ctype.h>
  42. #include <stdio.h>
  43. #include   <dos.h>
  44.  
  45. #define FALSE     0
  46. #define TRUE     !FALSE
  47.  
  48. /* common variables */
  49. extern int p_timestamp;
  50. extern int p_baud;
  51. extern int p_port;
  52. extern int port_base;
  53. char using_combios;
  54. char ints_active;
  55.  
  56. #define promptlen 4;
  57.  
  58. #define WAIT 1
  59.  
  60. int ars,point,ppoint,i,tc;
  61. unsigned int pnum;
  62. int old_more;
  63.  
  64. char token[129];
  65. char buf[128];
  66. int ctype;
  67. int ltype;
  68. int first_com_g;
  69.       
  70. int tflag,ok,eh;
  71. #define true 1
  72. #define false 0
  73. int point;
  74.  
  75. enum ttypes { notoken,
  76.     alftoken,
  77.     numtoken,
  78.     qmarktoken,        /* ? */
  79.     cmarktoken,        /* ; */
  80.     qtmarktoken,        /* " */
  81.     errtoken}  typtoken;
  82.  
  83. char oplist[] = " ?;\"";
  84.  
  85. FILE *fin, *fout, *fopen();
  86. int in_open = 0;
  87. int out_open = 0;
  88.  
  89. union   REGS inregs, outregs;
  90.  
  91. int baud[] = {
  92.     110,150,300,600,1200,2400,4800,9600};
  93.  
  94. int sp_skip(i)
  95. int i;
  96. {
  97. int looping;
  98.         looping = true;
  99.         point = i;
  100.         while (looping) {
  101.             if (point >=ars) looping = false;
  102.             else  if(buf[point]==' ') point++;
  103.               else looping = false;
  104.     }
  105.         return(point<ars);
  106. }
  107.  
  108.  
  109.  
  110. ptoken(npoint)
  111. int npoint;
  112. {
  113. int i,looping,itemp;
  114. char c;
  115.         memset(token,' ',127);
  116.         looping = sp_skip(npoint);
  117.         ppoint = point;
  118.         pnum = 0;
  119.         tc = -1;
  120.         typtoken = notoken;
  121. /* get token first */
  122. /* check for opcodes */
  123.     if (looping) {
  124.        typtoken=errtoken;        /* once started, it isn't notoken */
  125.        c = buf[point];
  126.        if(c=='?') {
  127.         tc++;
  128.         point++;
  129.         looping = false;
  130.         typtoken=qmarktoken;
  131.         }
  132.        else if(c==';') {
  133.         tc++;
  134.         point++;
  135.         looping = false;
  136.         typtoken=cmarktoken;
  137.         }
  138.        else if(c=='"') {
  139.         point++;
  140.         looping = true;    /* keep looping */
  141.         typtoken=qtmarktoken;
  142.         }
  143.        }    
  144.     if (!looping) token[tc]=c;    /* opcode */
  145.  
  146.     if (typtoken==qtmarktoken) {
  147.  
  148. /* quoted string */
  149.       while (looping) {
  150.         c = buf[point];
  151.            if (c=='"')     { /* check for double delimiter */
  152.         if (point+1>=ars) {
  153.             looping=false;
  154.             point++;   /* skip past ' */
  155.             }
  156.         else if (buf[point+1]=='"') {   /* put a ' into the token */
  157.             tc++;
  158.             if (tc<127) token[tc] = c;
  159.             point+=2;  /* skip past both ' */
  160.             }
  161.         else {
  162.             looping=false;
  163.             point++;     /* skip past closing ' */
  164.             }
  165.         }
  166.        else {
  167.                    tc++;
  168.                    if (tc<127) token[tc] = c;
  169.                    point++;
  170.                    if (point>=ars) {
  171.              looping = false;
  172.             typtoken=errtoken;  /* no trailing ' */
  173.             }
  174.                     }
  175.            } /* while looping */
  176.       }  /* end if qtmark */
  177.     else  if (looping) {
  178. /* alphanumeric token */
  179.         typtoken=alftoken;
  180.         while (looping) {
  181.             c = buf[point];
  182.                    if ((isalnum(c)) || (c=='_') || (c=='.') || (c=='-') ||
  183.             (c=='/')||(c=='\\')) {
  184.                         tc++;
  185.                             if (tc<127) token[tc] = c;
  186.                             point++;
  187.                             if (point>=ars) looping = false;
  188.                             }
  189.                     else if (NULL==strchr(oplist,c)) {
  190.                 looping = false;
  191.                 typtoken=errtoken;
  192.                 }
  193.             else looping = false;    
  194.                 }
  195.         }
  196.     token[tc+1]='\0';
  197. /* now characterize it */
  198.     if (typtoken==alftoken) {  /* alphanumeric, check for numeric */
  199.     typtoken = numtoken;
  200.       for (i=0;i<tc;i++) { 
  201.             if (!isdigit(token[i])) {
  202.             typtoken=alftoken;
  203.             break;
  204.             }
  205.         }
  206.     }
  207. /* convert numeric tokens */
  208.     pnum=0;
  209.     if (typtoken==numtoken) 
  210.         for(i=0;i<=tc;i++) pnum = pnum*10 + token[i]-'0';
  211.  
  212. }
  213.  
  214.  
  215. pcom()
  216. {
  217. int match, looking, i;
  218.         looking = true;
  219.         ctype = 1+ (int) c_fnull ;
  220.         while (comtext[ctype].c[0]!='\0' && (looking==1)) {
  221.             match = true;
  222.             i = 0;
  223.             while ((i <= tc) && (match==1)) {
  224.                match = (toupper(token[i]) == comtext[ctype].c[i]);
  225.                 i++;
  226.               }
  227.             if (match==1) looking = false;
  228.             else ctype++;
  229.           }
  230.       }
  231.  
  232. writedol(i)
  233. int i;
  234. {
  235.  int j;
  236.         i = i + promptlen;
  237.         for (j=0;j<i;j++) cprintf(" ");
  238.         cprintf("^");
  239. }
  240.  
  241. writeeh(i)
  242. int i;
  243. {
  244.         writedol(i);
  245.         cprintf(" Error");
  246.     pbreak();
  247. }
  248.  
  249. peh()
  250. {
  251.     eh=true;
  252.     ok=false;
  253. }
  254.  
  255. doit()
  256. {
  257. int i,j,k,ii;
  258. unsigned int pnumt;
  259. char com_out[255];
  260. unsigned char cmdnum, cmdtask;
  261. unsigned int  com_out_l;
  262.  
  263.         switch (ctype) {
  264.           case c_lnull:
  265.               ok = false;
  266.               eh = true;
  267.         break;
  268.           
  269.       case c_help:
  270.         ptoken(point);
  271.              printf("Commands are:\n");
  272.          i=(numcom)/8;
  273.          k=1;
  274.          for (j=0;j<i;j++) {
  275.             for (ii=0;ii<8;ii++,k++) printf("%.8s ",comtext[k].c);
  276.             printf("\n");
  277.             }
  278.          for (ii=0;ii<((numcom) % 8);ii++,k++)
  279.             printf("%.8s ",comtext[k].c);
  280.          printf("\n");
  281.          break;
  282.  
  283.           case c_baud:
  284.         ptoken(point);
  285.         if (typtoken==notoken) 
  286.             printf("Baud rate is %u\n",p_baud);
  287.         else if (typtoken!=numtoken) peh();
  288.         else{
  289.             for(i = 0; i < 8; i++){
  290.                 if(pnum == baud[i])
  291.                     break;
  292.             }
  293.             if(i == 8){
  294.                 printf("Error: Baudrate %d is not supported.\n",pnum);
  295.                 break;
  296.             }
  297.             if(using_combios){
  298.                 /* set up port for 8 bits, 1 stop, no parity */
  299.                 inregs.h.al = (i << 5) + 3;    
  300.                 inregs.h.ah = 0;    /* Initialize */
  301.                 inregs.x.dx = p_port;
  302.                 int86(0x14,&inregs,&outregs);
  303.             }
  304.             else{
  305.                 serlbaud(p_port,i);
  306.             }
  307.             p_baud = pnum;
  308.         }
  309.         break;
  310.  
  311.           case c_port:
  312.         ptoken(point);
  313.         if (typtoken==numtoken) {
  314.  
  315.         /* check for supported COM port */
  316.  
  317.             inregs.h.ah = 4;    /* Inquiry */
  318.             inregs.x.dx = pnum;
  319.             int86(0x14,&inregs,&outregs);
  320.             if(outregs.x.ax == 0xaa55){
  321.                 if(ints_active)
  322.                     serldone();
  323.                 ints_active = FALSE;
  324.                 using_combios = TRUE;
  325.                 p_port = pnum;
  326.                 break;
  327.             }
  328.             else if(pnum == 0 || pnum == 1) {
  329.                 if(ints_active)
  330.                     serldone();
  331.                 serlinit(pnum);
  332.                 ints_active = TRUE;
  333.                 using_combios = FALSE;
  334.                 p_port = pnum;
  335.  
  336.                 /* reset base adr for CD sampler */
  337.  
  338.                 if(p_port){
  339.                     set_base(0x2f8);    /* com2 */
  340.                 }
  341.                 else{
  342.                     set_base(0x3f8);    /* com1 */
  343.                 }
  344.                 break;
  345.             }
  346.             else{
  347.                 printf("Error: Invalid or unsupported port number %d.\n",pnum);
  348.             } 
  349.         }
  350.         else if (typtoken!=notoken) {
  351.             peh();
  352.             break;
  353.             }
  354.         printf ("Port is %u (COM%u)\n",p_port,p_port+1);
  355.         break;
  356.  
  357.       case c_end:
  358.       case c_quit:
  359.         serldone();
  360.         tick_stop();
  361.         dump_data();
  362.         exit(0);
  363.         break;
  364.  
  365.       case c_time:
  366.         p_timestamp = !p_timestamp;
  367.         if (p_timestamp) printf("Screen timestamping is now on\n");
  368.         else printf("Screen timestamping is now off\n");
  369.         break;
  370.  
  371.       case c_base:
  372.         ptoken(point);
  373.         if (typtoken==numtoken) {
  374.             port_base = pnum;
  375.             set_base(port_base);
  376.         }
  377.         else if (typtoken!=notoken) {
  378.             peh();
  379.             break;
  380.         }
  381.         printf ("Port base address is %Xh\n",port_base);
  382.         break;
  383.     
  384.       default: 
  385.         cprintf("*** internal error, parser default case\n");
  386.         break;
  387.     }
  388.     if(typtoken==cmarktoken) point--;
  389. }
  390. parse(flag,s)
  391. int flag;            /* command line flag */
  392. char *s;
  393. {
  394.     ok = true;
  395.     eh = false;
  396.     if (flag) 
  397.       strcpy (buf,s);
  398.     else gets(buf);
  399.     ars = strlen(buf);
  400.                 
  401.         ptoken(0);
  402.     while ((ok) && (typtoken!=notoken)){
  403.         if (typtoken==qmarktoken) {
  404.             ctype=(int) c_help;
  405.             doit();
  406.             }
  407.         else if ((typtoken==alftoken)||(typtoken==numtoken)) {
  408.                 pcom();
  409.                   if (ctype == (int) c_lnull) {
  410.                           ok = false;
  411.                           eh = true;
  412.                           }
  413.             else doit();
  414.             }
  415.         else if (typtoken==cmarktoken) ;  /* do nothing */
  416.  
  417.         else {
  418.             ok = false;
  419.             eh = true;
  420.             }
  421.         if (ok) ptoken(point);
  422.         else if (eh) writeeh(ppoint);
  423.                 }
  424. }
  425.  
  426.  
  427. pbreak()
  428. {
  429.     cprintf("\r\n");
  430. }
  431.  
  432.